package com.touchstone.synchdata.common.util;

import cn.hutool.core.util.StrUtil;

/**
 * @Description : sql相关工具类
 * @Author : hunter
 * @Date: 2018-03-22 18:12
 **/
public class SqlUtils {

    /**
     * 模糊查询中需要特殊处理的符号
     */
    private static String[] SQL_SPECIAL_CHARACTERS = {"%", "_"};

    /**
     * 功能描述: 模糊查询中，keyword需要处理特殊符号
     *
     * @param: keyword 原来的字符串
     * @return: 处理后的字符串
     * @auther: hunter
     * @date: 2018/3/22 17:58
     */
    public static String handleKeywordSpecialCharacter(String keyword) {
        if (StrUtil.isEmpty(keyword)) {
            return "";
        }
        keyword = keyword.trim();
        boolean flag;
        StringBuilder sb = new StringBuilder();
        for (int i = 0, length = keyword.length(); i < length; i++) {
            flag = false;
            for (String s : SQL_SPECIAL_CHARACTERS) {
                if (String.valueOf(keyword.charAt(i)).equals(s)) {
                    flag = true;
                }
            }
            if (flag) {
                sb.append("\\");
            }
            sb.append(keyword.charAt(i));
        }
        return sb.toString();
    }

}
